﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VehicleScale.Models;

namespace VehicleScale.EntityMappers
{
    public class ProductMapper
    {
        #region Methods With Relation
        public static ProductModel ToModel(Product entity)
        {
            if (entity == null) return null;

            ProductModel model = new ProductModel()
            {
                ProductID = entity.ProductID,
                ProductName = entity.ProductName,
                CategoryID = entity.CategoryID,
                Description = entity.Description,
                Unit = entity.Unit,
                Category = CategoryMapper.ToModel(entity.Category, 0),
                VehicleScales = new List<VehicleScaleModel>()
            };

            foreach (var item in entity.VehicleScales)
            {
                model.VehicleScales.Add(VehicleScaleMapper.ToModel(item, 0));
            }

            return model;
        }

        public static Product ToEntity(Product entity, ProductModel model)
        {
            if (model == null) return null;

            entity.ProductID = model.ProductID;
            entity.ProductName = model.ProductName;
            entity.CategoryID = model.CategoryID;
            entity.Description = model.Description;
            entity.Unit = model.Unit;
            
            return entity;
        } 
        #endregion

        #region Methods Without Relation
        public static ProductModel ToModel(Product entity, int childLevel)
        {
            if (entity == null) return null;

            ProductModel model = new ProductModel()
            {
                ProductID = entity.ProductID,
                ProductName = entity.ProductName,
                CategoryID = entity.CategoryID,
                Description = entity.Description,
                Unit = entity.Unit,
                //Category = CategoryMapper.ToModel(entity.Category)
            };

            if (childLevel > 0)
            {
                model.VehicleScales = new List<VehicleScaleModel>();
                foreach (var item in entity.VehicleScales)
                {
                    model.VehicleScales.Add(VehicleScaleMapper.ToModel(item, childLevel - 1));
                }
            }

            return model;
        }

        public static Product ToEntity(Product entity, ProductModel model, int childLevel)
        {
            if (model == null) return null;

            entity.ProductID = model.ProductID;
            entity.ProductName = model.ProductName;
            entity.CategoryID = model.CategoryID;
            entity.Description = model.Description;
            entity.Unit = model.Unit;
            //entity.Category = CategoryMapper.ToEntity(entity.Category, model.Category);

            if (childLevel > 0)
            {
                entity.VehicleScales = new System.Data.Linq.EntitySet<VehicleScale>();
                foreach (var item in model.VehicleScales)
                {
                    entity.VehicleScales.Add(VehicleScaleMapper.ToEntity(new VehicleScale(), item));
                }
            }

            return entity;
        }
        #endregion
    }
}
